import { success } from '../common/ResResult'
import { Context } from 'koa'
import { get, post } from '../decorator/reqmethoddecorator'
import BookDao from '../modules/books/dao/BookDao'
import { Controller } from '../decorator/controllerdecorator'
// TS 装饰器 重构Koa 路由中的方法装饰器
@Controller('/booksmodule')
class BooksController {
  @get('/findBooksByThirdCtgyId/:thirdctgyid/:pageNo/:pageSize')
  async findBooksByThirdCtgyId(ctx: Context) {
    const { thirdctgyid, pageNo, pageSize } = ctx.params
    const offset = (pageNo - 1) * pageSize
    ctx.body = success(await BookDao.findBooksByThirdCtgyId(thirdctgyid, offset, parseInt(pageSize)))
  }

  @get('/findBookRisefall/:risefall/:thirdctgyid')
  async findBookRisefall(ctx: Context) {
    const { risefall, thirdctgyid } = ctx.params
    ctx.body = success(await BookDao.findBookRisefall(thirdctgyid, risefall))
  }

  @get('/findBookDetail/:ISBN')
  async findBookDetail(ctx: Context) {
    const { ISBN } = ctx.params
    ctx.body = success(await BookDao.findBookDetail(ISBN))
  }

  @get('/findBookByAutoCompKeyword/:autocompkeyword')
  async findBookByAutoCompKeyword(ctx: Context) {
    const { autocompkeyword } = ctx.params
    const bookList = await BookDao.findBooksByAutoCompKeyword(autocompkeyword)
    ctx.body = success(bookList)
  }

  @get('/findPublishersByAutoCompKey/:autocompkeyword')
  async findPublishersByAutoCompKey(ctx: Context) {
    const { autocompkeyword } = ctx.params
    const publishers = await BookDao.findPublishersByAutoCompKey(autocompkeyword)
    ctx.body = success(publishers)
  }
  @post('/findBksByPublishIds')
  async findBksByPublishIds(ctx: Context) {
    const { publishIds } = ctx.request.body
    const books = await BookDao.findBksByPublishIds(publishIds)
    ctx.body = success(books)
  }
}
